package cn.hp.cms.dao.impl;

import cn.hp.cms.dao.UserDao;
import cn.hp.cms.entity.User;
import cn.hp.cms.utils.DBUtil;
import cn.hp.cms.utils.PageData;
import cn.hutool.core.util.StrUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public boolean selectOneByNameAndPassword(String userName, String password) {
        String sql = "select id from c_user where user_name = ? and password =? and is_del = 1 ";
        Object [] objs = {userName , password};
        ResultSet select = DBUtil.select(sql, objs);
        boolean flag = false ;
        try {
            flag = select.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return flag;
    }

    @Override
    public List<User> selectAll() {
        String sql = "select * from c_user ";
        Object [] obj = {};
        ResultSet select = DBUtil.select(sql, obj);
//        创建一个list集合用于返回
        List<User> users = new ArrayList<>();
        try {
            while (select.next()){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
                Date parse = sdf.parse(select.getString(8));
                Date parse2 = sdf.parse(select.getString(9));

                User user = new User(select.getInt(1)
                        ,select.getString(2)
                        ,select.getString(3)
                        ,select.getString(4)
                        ,select.getString(5)
                        ,select.getInt(6)
                        ,select.getInt(7)
                        , parse
                        ,parse2
                );
                users.add(user);
            }
        }catch (SQLException | ParseException e){
            e.printStackTrace();
        }
        return users;
    }

//    获取到c_user表中的全部条数
    @Override
    public int selectAllCount(User user) {
        String sql = "select count(id) from c_user where 1=1 ";
        if (!StrUtil.isEmpty(user.getUserName())){  // 不为空 并且长度大于0 才加条件
            sql += "and user_name like '%" + user.getUserName() + "%'";
        }
        if (!StrUtil.isEmpty(user.getRealName())){  // 不为空 并且长度大于0 才加条件
            sql += "and real_name like '%" + user.getRealName() + "%'";
        }
        if (user.getType() != 0 ){  // 不为空 并且长度大于0 才加条件
            sql += "and type = " + user.getType() ;
        }

        Object object[] ={} ;
        ResultSet select = DBUtil.select(sql, object); // 得到1行1列的值
        int count = 0 ;
        try{
            while (select.next()){
                count = select.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return count;
    }

    @Override
    public List<User> selectAllByLimit(PageData pageData, User user) {
//        第一页 从 0~10  limit 0 ,10   (1-1)*10
//        第二页 从 10,10   (2-1)*10    (3-1)*10
//        判断user中查询条件的内容 然后进行sql语句的拼接
        String sql = "select * from c_user where 1 = 1 ";
//        合并sql语句
        if (!StrUtil.isEmpty(user.getUserName())){  // 不为空 并且长度大于0 才加条件
            sql += " and user_name like '%" + user.getUserName() + "%'";
        }
        if (!StrUtil.isEmpty(user.getRealName())){  // 不为空 并且长度大于0 才加条件
            sql += " and real_name like '%" + user.getRealName() + "%'";
        }
        if (user.getType() != 0 ){  // 不为空 并且长度大于0 才加条件
            sql += " and type = " + user.getType() ;
        }
        sql+= " limit "+(pageData.getPage()-1)*pageData.getLimit() +","+ pageData.getLimit();
        Object [] objects = {};
        ResultSet select = DBUtil.select(sql, objects);
//        创建一个list集合用于返回
        List<User> users = new ArrayList<>();
        try {
            while (select.next()){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
                Date parse = sdf.parse(select.getString(8));
                Date parse2 = sdf.parse(select.getString(9));

                User user1 = new User(select.getInt(1)
                        ,select.getString(2)
                        ,select.getString(3)
                        ,select.getString(4)
                        ,select.getString(5)
                        ,select.getInt(6)
                        ,select.getInt(7)
                        ,parse
                        ,parse2
                );
                users.add(user1);
            }
        }catch (SQLException | ParseException e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return users;
    }

//    通过id修改用户密码为123456
    @Override
    public int updatePasswordById(int id) {
        String sql = "update c_user set password = '123456' where id = ?";
        Object [] objs = {id};
        int update = DBUtil.update(sql, objs);
        return update;
    }

//    通过id删除user表数据
    @Override
    public int deleteById(String[] ids) {
        String sql = "delete from c_user where id in ( ? " ;
        for (int i = 1 ; i < ids.length ; i++){ // 从第二次开始加入 ?
            sql += ", ? ";
        }
        sql += ")" ;
        int update = DBUtil.update(sql, ids);
        return update;
    }

//    通过用户名查询用户
    @Override
    public User selectByName(String userName) {
        String sql = "select user_name from c_user where user_name = ?";
        Object [] objs = {userName};
        ResultSet resultSet = DBUtil.select(sql, objs);
        User user = null ;
        try {
            if (resultSet.next()){
                user = new User(resultSet.getString(1));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return user;
    }

//    添加用户
    @Override
    public int Add(User user) {
        String sql = "insert into c_user(user_name , password ,real_name ,type ,is_del ,create_time ,modify_time) value ( ?,?,?,?,1,now(),now()) ";
        Object [] objs = {user.getUserName(),user.getPassword(),user.getRealName(),user.getType()};
        int update = DBUtil.update(sql, objs);
        return update;
    }

    @Override
    public int updatePasswordByName(String username, String newPassword) {
        String sql = "update c_user set password = ? where user_name = ?";
        Object [] objs = {newPassword , username};
        int update = DBUtil.update(sql, objs);
        return update;
    }

    @Override
    public int updateImgByName(String user, String imgPath) {
        String sql = "update c_user set img = ? where user_name = ?";
        Object [] objects = {imgPath , user} ;
        int update = DBUtil.update(sql, objects);
        return update;
    }

//    通过用户名查询头像值
    @Override
    public String selectImgByName(String userName) {
        String sql = "select img from c_user where user_name = ?" ;
        Object [] objects = {userName};
        ResultSet select = DBUtil.select(sql, objects);
        String img = "";
        try {
            if (select.next()){
                img = select.getString(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return img;
    }
}
